package com.chen.user.service;


import com.chen.user.common.Result;
import com.chen.user.entity.User;
import com.chen.user.entity.UserLogin;
import com.chen.user.mapper.UserPersonalMapper;
import com.chen.user.request.UserRequest;
import com.chen.user.response.UserResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserPersonalService {


    @Autowired
    private UserPersonalMapper userPersonalMapper;

//    public UserResponse getUser(Integer id) {
//       // User user = userPersonalMapper.selectById(id);
//        UserResponse userResponse = new UserResponse();
//        userResponse.setId(user.getId());
//        userResponse.setUserName(user.getUsername());
//        userResponse.setNickName(user.getNickname());
//        userResponse.setEmail(user.getEmail());
//        userResponse.setPhone(user.getPhone());
//        return userResponse;
//    }

    public Result updateUser(UserRequest userRequest) {


     //   int count = userPersonalMapper.updateById(user);

       int count= userPersonalMapper.updateUser(userRequest);
        if(count==0)
        {
            return Result.error("400","设置失败");
        }
        return Result.success("设置成功");
    }

    public Result register(UserLogin user) {

       UserLogin userLogin= userPersonalMapper.register(user);
       if(userLogin!=null)

       {
           if(userLogin.getUsername()!=null && userLogin.getPassword()!=null){
               return Result.error("400","用户已经存在");
           }


       }

     int count=  userPersonalMapper.insertUser(user);
       if(count==0)
       {
           return Result.error("400","注册失败");
       }
       return Result.success("注册成功");
    }

    public Result login(UserLogin user) {

      UserLogin userLogin=  userPersonalMapper.login(user);
      if(userLogin==null)
      {
          return Result.error("400","登录失败");
      }

      return Result.success("登录成功");

    }
}
